﻿using System.Web.Mvc;
using Raven.Client;

namespace Udbjorg.Net.Logic.Raven
{
    public class RavenController : Controller
    {
        public IDocumentSession DocumentSession { get; set; }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            if (filterContext.IsChildAction)
                return;
            DocumentSession = DocumentStore.Instance.OpenSession();
            base.OnActionExecuting(filterContext);
        }

        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            if (DocumentSession == null)
                return;

            if (filterContext.IsChildAction)
                return;

            if (DocumentSession != null && filterContext.Exception == null)
                DocumentSession.SaveChanges();
            DocumentSession.Dispose();
            base.OnActionExecuted(filterContext);
        }
    }
}
